package com.example.bleuesprit.parkAndroidClient;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bleuesprit.parkAndroidClient.global.GlobalArgs;
import com.example.bleuesprit.parkAndroidClient.net.NetUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends Activity {


    private GlobalArgs globalArgs=(GlobalArgs)getApplication();

    SharedPreferences sharedPreference;

    EditText loginAccount_EdT;
    EditText loginPassword_EdT;

    CheckBox autoLogin_Ck;
    CheckBox rememberAccount_Ck;

    Button login_Bnt;
    TextView register_TV;
    TextView forgetPassWord_TV;


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x123) {
                // 登录信息返回结果
                Log.i("LoginActivity",msg.getData().getString("result"));
                try {
                    JSONObject jsonObject=new JSONObject(msg.getData().getString("result"));

                    //Toast.makeText(LoginActivity.this, jsonObject.getString("result"), Toast.LENGTH_LONG).show();
                    if( jsonObject.getString("result").equals("fail"))
                    {
                        loginAccount_EdT.setText("");
                        loginPassword_EdT.setText("");
                        Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_LONG).show();
                        return;
                    }




                    //设置共享配置文件
                    Editor edit = sharedPreference.edit();
                    edit.putString("account", loginAccount_EdT.getText().toString());
                    edit.putString("name", jsonObject.getString("name"));
                    edit.putString("telNumberd", jsonObject.getString("telNumberd"));
                    edit.putString("licence", jsonObject.getString("licence"));
                    if(autoLogin_Ck.isChecked())
                    {


                        edit.putString("password", loginPassword_EdT.getText().toString());
                        edit.putBoolean("autoLogin", true);

                    }
                    else
                    {
                        edit.putBoolean("autoLogin", false);
                        if(rememberAccount_Ck.isChecked())
                        {

                            edit.putString("account", loginAccount_EdT.getText().toString());
                            edit.putBoolean("rememberAccount", true);
                        }
                        else
                        {
                            edit.putBoolean("rememberAccount", false);
                        }
                    }
                    edit.commit();

                    //globalArgs.userAccount = loginAccount_EdT.getText().toString();

                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);

                    Bundle data=new Bundle();
                    data.putString("userAccount",loginAccount_EdT.getText().toString());
                    intent.putExtras(data);
                    startActivity(intent);

                    finish();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //GlobalArgs obj=(GlobalArgs)getApplication();



        sharedPreference=this.getSharedPreferences("appconfig", MODE_PRIVATE);

        setContentView(R.layout.activity_login);
        loginAccount_EdT=(EditText) findViewById(R.id.loginaccount);
        loginPassword_EdT=(EditText)findViewById(R.id.loginpassword);
        autoLogin_Ck=(CheckBox)findViewById(R.id.login_autoLogin_checkbox);
        rememberAccount_Ck=(CheckBox)findViewById(R.id.login_rememberAccount);
        login_Bnt=(Button)findViewById(R.id.login);
        register_TV =(TextView)findViewById(R.id.register);
        forgetPassWord_TV=(TextView)findViewById(R.id.forgetPassWord);

        autoLogin_Ck.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                       rememberAccount_Ck.setChecked(autoLogin_Ck.isChecked());

                    }
                }
        );


        login_Bnt.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        doLogin(v);
                    }
                }
        );

        register_TV.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        doRegist(v);
                    }
                }
        );

        forgetPassWord_TV.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        doForgetPassWord(v);
                    }
                }
        );

        if(sharedPreference.getBoolean("autoLogin", false)){
            loginAccount_EdT.setText(sharedPreference.getString("account",""));
            loginPassword_EdT.setText(sharedPreference.getString("password",""));
            autoLogin_Ck.setChecked(true);
            rememberAccount_Ck.setChecked(true);
            doLogin(null);
        }

        if(sharedPreference.getBoolean("rememberAccount", false)){
            loginAccount_EdT.setText(sharedPreference.getString("account", ""));
            rememberAccount_Ck.setChecked(true);
        }

    }

    void doRegist(View v)
    {
        Intent intent=new Intent(this,RegistActivity.class);
        startActivity(intent);
    }

    void doLogin(View v)
    {
        JSONObject loginMessage=new JSONObject();
        try {
            loginMessage.put("account", loginAccount_EdT.getText().toString());
            loginMessage.put("pwd", loginPassword_EdT.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtil.sendToServer("login", loginMessage, handler);
    }


    void doForgetPassWord(View v)
    {
       Intent intent=new Intent(this,ForgetPassWordActivity.class);
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {


        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        return id == R.id.action_settings || super.onOptionsItemSelected(item);
    }

}

